<?php
/**
 * set service for web application
 * @var $app \micro\web\App
 */

require __DIR__ . '/common-services.php';

$app->set('view', function(\micro\web\App $app)
{
    $options = $app->config('view', []);
    $options['debug'] = $app->isDebug();
    $viewsPath = PROJECT_PATH . '/' . $options['viewsPath'];
    $layout = isset($options['layout']) ? $options['layout'] : '';
    $attributes = isset($options['attributes']) ? $options['attributes'] : [];

    return new \micro\web\SimpleRenderer($viewsPath, $layout, $attributes);
});

$app->set('input', function(\micro\web\App $app)
{
    return new \inhere\library\http\Request;
});

$app->set('output', function(\micro\web\App $app)
{
    return new \inhere\library\http\Response;
});

$app->set('onNotFound', function()
{
    $uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
    $method = $_SERVER['REQUEST_METHOD'];

    \Micro::logger()->warning("Page not found: $uri");

    \Micro::$app->view->renderPartial('404.php', [
        'uri' => $uri,
        'method' => $method,
    ])->send();
});

// can also use property add service.
$app->onBeforeRun = function(\micro\web\App $app)
{
    $uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);

    $app->logger->debug("App run start. URL: $uri,Time: " . microtime(true));
};

$app->set('onAfterRun', function(\micro\web\App $app)
{
    $app->logger->debug('App run end. Time:' . microtime(true));
});

$app->onAppStop = function(\micro\web\App $app)
{
    $app->logger->debug('App stopped. Time:' . microtime(true));

    \inhere\library\utils\LiteLogger::flushAll();
};
